Root.js ➔ describe(ꞌRootꞌ)   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 423

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 423
rs 8.2857

3 Functions

Rating   Name   Duplication   Size   Complexity  
A Root.js ➔ ... ➔ it(ꞌtoJSONꞌ) 0 5 1
B Root.js ➔ ... ➔ it(ꞌBuildꞌ) 0 141 1
A Root.js ➔ ... ➔ it(ꞌCreate with JSONꞌ) 0 4 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
var chai = require('chai');
2
chai.use(require('chai-json-schema'));
3
chai.tv4.banUnknown = true;
4
5
var assert = chai.assert,
6
    GedcomXSchema = require('gedcomx-json-schema'),
7
    GedcomX = require('../../');
8
9
describe('Root', function(){
10
  
11
  var fullJSON = {
12
    id: 'gedcomx',
13
    lang: 'en',
14
    description: '#sd1',
15
    attribution: { 
16
      changeMessage: 'It changed',
17
      contributor: { resource: 'https://myapp.com/contributor'},
18
      created: 1111338494969,
19
      creator: { resource: 'https://myapp.com/creator'},
20
      modified: 1111338494969
21
    },
22
    persons: [
23
      {
24
        id: 'testPerson',
25
        principal: true,
26
        private: false,
27
        gender: {
28
          type: 'http://gedcomx.org/Female'
29
        },
30
        names: [
31
          {
32
            type: 'http://gedcomx.org/BirthName',
33
            nameForms: [
34
              {
35
                fullText: 'Joanna Hopkins'
36
              }
37
            ]
38
          }  
39
        ],
40
        facts: [
41
          {
42
            type: 'http://gedcomx.org/Birth',
43
            date: {
44
              formal: '+2001-04-09'
45
            },
46
            place: {
47
              original: 'Farm house'
48
            }
49
          }  
50
        ]
51
      } 
52
    ],
53
    relationships: [
54
      {
55
        type: 'http://gedcomx.org/Couple',
56
        person1: {
57
          resource: 'http://identifier/for/person/1'
58
        },
59
        person2: {
60
          resource: 'http://identifier/for/person/2'
61
        },
62
        facts: [
63
          {
64
            type: 'http://gedcomx.org/Marriage'
65
          }
66
        ]
67
      }  
68
    ],
69
    sourceDescriptions: [
70
      {
71
        id: 'sd1',
72
        resourceType: 'http://some/type',
73
        citations: [
74
          {
75
            lang: 'en',
76
            value: 'Long source citation'
77
          }
78
        ],
79
        mediaType: 'book',
80
        about: 'http://a/resource',
81
        mediator: {
82
          resource: 'http://mediator'
83
        },
84
        sources: [
85
          {
86
            description: 'http://source/reference'
87
          }
88
        ],
89
        analysis: {
90
          resource: 'http://analysis'
91
        },
92
        componentOf: {
93
          description: 'http://container'
94
        },
95
        titles: [
96
          {
97
            lang: 'en',
98
            value: 'Title'
99
          },
100
          {
101
            lang: 'es',
102
            value: 'Titulo'
103
          }
104
        ],
105
        notes: [
106
          {
107
            subject: 'Note',
108
            text: 'Some note text'
109
          }
110
        ],
111
        attribution: {
112
          created: 1234578129
113
        },
114
        rights: [
115
          {
116
            resource: 'https://some/right'
117
          }
118
        ],
119
        coverage: [{
120
          temporal: {
121
            formal: '+2015'
122
          },
123
          spatial: {
124
            original: 'A place'
125
          }
126
        }],
127
        descriptions: [
128
          {
129
            value: 'A description'
130
          }
131
        ],
132
        identifiers: {
133
          $: 'identifier'
134
        },
135
        created: 1000000,
136
        modified: 11111111,
137
        repository: {
138
          resource: 'http://repository'
139
        }
140
      }
141
    ],
142
    agents: [
143
      {
144
        id: 'agent',
145
        identifiers: {
146
          $: 'ident'
147
        },
148
        names: [
149
          {
150
            lang: 'en',
151
            value: 'Name'
152
          }  
153
        ],
154
        homepage: {
155
          resource: 'http://homepage'
156
        },
157
        openid: {
158
          resource: 'http://openid'
159
        },
160
        accounts: [
161
          {
162
            accountName: 'jimbo'
163
          }  
164
        ],
165
        emails: [
166
          {
167
            resource: 'http://email'
168
          }  
169
        ],
170
        phones: [
171
          {
172
            resource: 'http://phone'
173
          }  
174
        ],
175
        addresses: [
176
          {
177
            value: 'big long address',
178
            postalCode: '123456'
179
          }  
180
        ],
181
        person: {
182
          resource: 'http://person'
183
        }
184
      }  
185
    ],
186
    events: [
187
      {
188
        type: 'http://gedcomx.org/Marriage',
189
        date: {
190
          formal: '+2002-06-06'
191
        },
192
        place: {
193
          original: 'Her town, MN'
194
        },
195
        roles: [
196
          {
197
            person: {
198
              resource: 'http://groom'
199
            },
200
            type: 'http://gedcomx.org/Participant'
201
          }
202
        ]
203
      }  
204
    ],
205
    documents: [
206
      {
207
        type: 'http://gedcomx.org/Abstract',
208
        extracted: false,
209
        textType: 'plain',
210
        text: 'Lots of text',
211
        attribution: {
212
          created: 123456789
213
        }
214
      }  
215
    ],
216
    places: [
217
      {
218
        names : [ 
219
          {
220
            lang : 'en',
221
            value : 'Pope\'s Creek, Westmoreland, Virginia, United States'
222
          }
223
        ],
224
        type : 'http://identifier/for/the/place/type',
225
        place : { resource : 'http://place' },
226
        jurisdiction : { resource : 'http://jurisdiction' },
227
        latitude : 27.9883575,
228
        longitude : 86.9252014,
229
        temporalDescription : { 
230
          formal: '+1899-01-04'
231
        },
232
        spatialDescription : {
233
          resource : 'http://uri/for/KML/document'
234
        }
235
      }
236
    ],
237
    recordDescriptors: [
238
      {
239
        id: 'rd',
240
        lang: 'en-US',
241
        fields: [{
242
          originalLabel: 'Name',
243
          descriptions: [
244
            { lang: 'en-US', value: 'Full Name' }
245
          ],
246
          values: [{
247
            optional: true,
248
            type: 'http://gedcomx.org/Original',
249
            labelId: 'name',
250
            displayLabels: [
251
              { lang: 'en-US', value: 'Name' }
252
            ]
253
          }]
254
        }]
255
      }
256
    ],
257
    collections: [
258
      {
259
        id: 'collection',
260
        lang: 'en-US',
261
        content: [{
262
          resourceType: 'http://gedcomx.org/Record',
263
          count: 183429102,
264
          completeness: .8237
265
        }],
266
        title: 'Collection Title',
267
        size: 183429102,
268
        attribution: { 
269
          contributor: { resource: 'https://myapp.com/contributor'},
270
          created: 1111338494969,
271
          creator: { resource: 'https://myapp.com/creator'},
272
          modified: 1111338494969
273
        }
274
      }  
275
    ]
276
  };
277
  
278
  it('Create with JSON', function(){
279
    var gedx = GedcomX(fullJSON);
280
    tests(gedx);
281
  });
282
  
283
  it('Build', function(){
284
    var gedx = GedcomX()
285
      .setId('gedcomx')
286
      .setLang('en')
287
      .setDescription('#sd1')
288
      .setAttribution(GedcomX.Attribution()
289
        .setChangeMessage('It changed')
290
        .setContributor({ resource: 'https://myapp.com/contributor'})
291
        .setCreated(1111338494969)
292
        .setCreator({ resource: 'https://myapp.com/creator'})
293
        .setModified(1111338494969))
294
      .addPerson(
295
        GedcomX.Person()
296
          .setId('testPerson')
297
          .setPrivate(true)
298
          .setPrincipal(true)
299
          .setGender(GedcomX.Gender().setType('http://gedcomx.org/Female'))
300
          .addName(GedcomX.Name().addNameForm(GedcomX.NameForm().setFullText('Joanna Hopkins')))
301
          .addFact(GedcomX.Fact().setDate(GedcomX.Date().setFormal('+2001-04-09')).setPlace(GedcomX.PlaceReference().setOriginal('Farm house')))
302
      )
303
      .addRelationship(
304
        GedcomX.Relationship()
305
          .setType('http://gedcomx.org/Couple')
306
          .setPerson1(GedcomX.ResourceReference().setResource('http://identifier/for/person/1'))
307
          .setPerson2(GedcomX.ResourceReference().setResource('http://identifier/for/person/2'))
308
          .addFact(GedcomX.Fact().setType('http://gedcomx.org/Marriage'))  
309
      )
310
      .addSourceDescription(
311
        GedcomX.SourceDescription()
312
          .setId('sd1')
313
          .setResourceType('http://some/type')
314
          .addCitation(GedcomX.SourceCitation().setLang('en').setValue('Long source citation'))
315
          .setMediaType('book')
316
          .setAbout('http://a/resource')
317
          .setMediator(GedcomX.ResourceReference().setResource('http://mediator'))
318
          .addSource(GedcomX.SourceReference().setDescription('http://source/reference'))
319
          .setAnalysis(GedcomX.ResourceReference().setResource('http://analysis'))
320
          .setComponentOf(GedcomX.SourceReference().setDescription('http://container'))
321
          .addTitle(GedcomX.TextValue().setLang('en').setValue('Title'))
322
          .addTitle(GedcomX.TextValue().setLang('es').setValue('Titulo'))
323
          .addNote(GedcomX.Note().setSubject('Note').setText('Some note text'))
324
          .setAttribution(GedcomX.Attribution().setCreated(1234578129))
325
          .addRight(GedcomX.ResourceReference().setResource('https://some/right'))
326
          .addCoverage(
327
            GedcomX.Coverage()
328
              .setTemporal(GedcomX.Date().setFormal('+2015'))
329
              .setSpatial(GedcomX.PlaceReference().setOriginal('A place'))
330
          )
331
          .addDescription(GedcomX.TextValue().setValue('A description'))
332
          .setIdentifiers(GedcomX.Identifiers({
333
            $: 'identifier'
334
          }))
335
          .setCreated(1000000)
336
          .setModified(11111111)
337
          .setRepository(GedcomX.ResourceReference().setResource('http://repository'))  
338
      )
339
      .addAgent(
340
        GedcomX.Agent()
341
          .setId('agent')
342
          .setIdentifiers(GedcomX.Identifiers({$:'ident'}))
343
          .addName(GedcomX.TextValue().setLang('en').setValue('Name'))
344
          .setHomepage(GedcomX.ResourceReference().setResource('http://homepage'))
345
          .setOpenid(GedcomX.ResourceReference().setResource('http://openid'))
346
          .addAccount(GedcomX.OnlineAccount().setAccountName('jimbo'))
347
          .addEmail(GedcomX.ResourceReference().setResource('http://email'))
348
          .addPhone(GedcomX.ResourceReference().setResource('http://phone'))
349
          .addAddress(GedcomX.Address().setValue('big long address').setPostalCode('123456'))
350
          .setPerson(GedcomX.ResourceReference().setResource('http://person'))  
351
      )
352
      .addEvent(
353
        GedcomX.Event()
354
          .setType('http://gedcomx.org/Marriage')
355
          .setDate(GedcomX.Date().setFormal('+2002-06-06'))
356
          .setPlace(GedcomX.PlaceReference().setOriginal('Her town, MN'))
357
          .addRole(GedcomX.EventRole()
358
            .setType('http://gedcomx.org/Participant')
359
            .setPerson(GedcomX.ResourceReference().setResource('http://groom')))  
360
      )
361
      .addDocument(
362
        GedcomX.Document()
363
        .setType('http://gedcomx.org/Abstract')
364
        .setExtracted(false)
365
        .setTextType('plain')
366
        .setText('Lots of text')
367
        .setAttribution(GedcomX.Attribution().setCreated(123456789))          
368
      )
369
      .addPlace(
370
        GedcomX.PlaceDescription()
371
          .addName(GedcomX.TextValue().setLang('en').setValue('Pope\'s Creek, Westmoreland, Virginia, United States'))
372
          .setType('http://identifier/for/the/place/type')
373
          .setPlace(GedcomX.ResourceReference({ resource : 'http://place' }))
374
          .setJurisdiction(GedcomX.ResourceReference({ resource : 'http://jurisdiction' }))
375
          .setLatitude(27.9883575)
376
          .setLongitude(86.9252014)
377
          .setTemporalDescription(GedcomX.Date({ 
378
            formal: '+1899-01-04'
379
          }))
380
          .setSpatialDescription(GedcomX.ResourceReference({
381
            resource : 'http://uri/for/KML/document'
382
          }))
383
      )
384
      .addRecordDescriptor(
385
        GedcomX.RecordDescriptor()
386
          .setId('rd')
387
          .setLang('en-US')
388
          .addField(
389
            GedcomX.FieldDescriptor()
390
              .setOriginalLabel('Name')
391
              .addDescription({ lang: 'en-US', value: 'Full Name' })
392
              .addValue(
393
                GedcomX.FieldValueDescriptor()
394
                  .setOptional(true)
395
                  .setType('http://gedcomx.org/Original')
396
                  .setLabelId('name')
397
                  .addDisplayLabel({ lang: 'en-US', value: 'Name' })
398
              )
399
          )
400
      )
401
      .addCollection(
402
        GedcomX.Collection()
403
          .setId('collection')
404
          .setLang('en-US')
405
          .addContent(
406
            GedcomX.CollectionContent()
407
              .setResourceType('http://gedcomx.org/Record')
408
              .setCount(183429102)
409
              .setCompleteness(.8237)  
410
          )
411
          .setTitle('Collection Title')
412
          .setSize(183429102)
413
          .setAttribution(
414
            GedcomX.Attribution()
415
              .setContributor({ resource: 'https://myapp.com/contributor'})
416
              .setCreated(1111338494969)
417
              .setCreator({ resource: 'https://myapp.com/creator'})
418
              .setModified(1111338494969)  
419
          )  
420
      );
421
    
422
    tests(gedx);
423
  });
424
  
425
  it('toJSON', function(){
426
    var gedx = GedcomX(fullJSON);
427
    assert.deepEqual(gedx.toJSON(), fullJSON);
428
    assert.jsonSchema(gedx.toJSON(), GedcomXSchema);
429
  });
430
  
431
});
432
433
function tests(gedx){
434
  assert.equal(gedx.getId(), 'gedcomx');
435
  assert.equal(gedx.getLang(), 'en');
436
  assert.equal(gedx.getDescription(), '#sd1');
437
  
438
  var attribution = gedx.getAttribution();
439
  assert.equal(attribution.getChangeMessage(), 'It changed', 'Change message not saved properly when created with JSON');
440
  assert.equal(attribution.getContributor().getResource(), 'https://myapp.com/contributor', 'Contributor not saved when created with JSON');
441
  assert.equal(attribution.getCreated().getTime(), 1111338494969, 'Created date not saved when created with JSON');
442
  assert.equal(attribution.getCreator().getResource(), 'https://myapp.com/creator', 'Creator not saved when created with JSON');
443
  assert.equal(attribution.getModified().getTime(), 1111338494969, 'Modified date not saved when created with JSON');
444
  
445
  assert.equal(gedx.getPersons().length, 1);
446
  var person = gedx.getPrincipalPerson();
447
  assert(person.getPrincipal());
448
  assert.equal(person.getGender().getType(), 'http://gedcomx.org/Female');
449
  assert.equal(person.getNames().length, 1);
450
  assert.equal(person.getFacts().length, 1);
451
  
452
  assert.equal(gedx.getRelationships().length, 1);
453
  var relationship = gedx.getRelationships()[0];
454
  assert.equal(relationship.getType(), 'http://gedcomx.org/Couple');
455
  assert.equal(relationship.getPerson1().getResource(), 'http://identifier/for/person/1');
456
  assert.equal(relationship.getPerson2().getResource(), 'http://identifier/for/person/2');
457
  assert.equal(relationship.getFacts().length, 1);
458
  assert.equal(relationship.getFacts()[0].getType(), 'http://gedcomx.org/Marriage');
459
  
460
  assert.equal(gedx.getSourceDescriptions().length, 1);
461
  var description = gedx.getSourceDescriptions()[0];
462
  assert.equal(description.getId(), 'sd1');
463
  assert.equal(description.getResourceType(), 'http://some/type');
464
  assert.equal(description.getCitations().length, 1);
465
  assert.equal(description.getCitations()[0].getLang(), 'en');
466
  assert.equal(description.getCitations()[0].getValue(), 'Long source citation');
467
  assert.equal(description.getMediaType(), 'book');
468
  assert.equal(description.getAbout(), 'http://a/resource');
469
  assert.equal(description.getMediator().getResource(), 'http://mediator');
470
  assert.equal(description.getSources().length, 1);
471
  assert.equal(description.getSources()[0].getDescription(), 'http://source/reference');
472
  assert.equal(description.getAnalysis().getResource(), 'http://analysis');
473
  assert.equal(description.getComponentOf().getDescription(), 'http://container');
474
  assert.equal(description.getTitles().length, 2);
475
  assert.equal(description.getTitles()[0].getLang(), 'en');
476
  assert.equal(description.getTitles()[0].getValue(), 'Title');
477
  assert.equal(description.getTitles()[1].getLang(), 'es');
478
  assert.equal(description.getTitles()[1].getValue(), 'Titulo');
479
  assert.equal(description.getNotes().length, 1);
480
  assert.equal(description.getNotes()[0].getSubject(), 'Note');
481
  assert.equal(description.getNotes()[0].getText(), 'Some note text');
482
  assert.equal(description.getAttribution().getCreated().getTime(), 1234578129);
483
  assert.equal(description.getRights().length, 1);
484
  assert.equal(description.getRights()[0].getResource(), 'https://some/right');
485
  assert.equal(description.getCoverage().length, 1);
486
  assert.equal(description.getCoverage()[0].getTemporal().getFormal(), '+2015');
487
  assert.equal(description.getCoverage()[0].getSpatial().getOriginal(), 'A place');
488
  assert.equal(description.getDescriptions().length, 1);
489
  assert.equal(description.getDescriptions()[0].getValue(), 'A description');
490
  assert.equal(description.getIdentifiers().identifiers.$, 'identifier');
491
  assert.equal(description.getCreated(), 1000000);
492
  assert.equal(description.getModified(), 11111111);
493
  assert.equal(description.getRepository().getResource(), 'http://repository');
494
  
495
  assert.equal(gedx.getAgents().length, 1);
496
  var agent = gedx.getAgents()[0];
497
  assert.equal(agent.getId(), 'agent');
498
  assert.equal(agent.getIdentifiers().identifiers.$, 'ident');
499
  assert.equal(agent.getNames().length, 1);
500
  assert.equal(agent.getNames()[0].getLang(), 'en');
501
  assert.equal(agent.getNames()[0].getValue(), 'Name');
502
  assert.equal(agent.getHomepage().getResource(), 'http://homepage');
503
  assert.equal(agent.getOpenid().getResource(), 'http://openid');
504
  assert.equal(agent.getAccounts().length, 1);
505
  assert.equal(agent.getAccounts()[0].getAccountName(), 'jimbo');
506
  assert.equal(agent.getEmails().length, 1);
507
  assert.equal(agent.getEmails()[0].getResource(), 'http://email');
508
  assert.equal(agent.getPhones().length, 1);
509
  assert.equal(agent.getPhones()[0].getResource(), 'http://phone');
510
  assert.equal(agent.getAddresses().length, 1);
511
  assert.equal(agent.getAddresses()[0].getValue(), 'big long address');
512
  assert.equal(agent.getAddresses()[0].getPostalCode(), '123456');
513
  assert.equal(agent.getPerson().getResource(), 'http://person');
514
  
515
  assert.equal(gedx.getEvents().length, 1);
516
  var event = gedx.getEvents()[0];
517
  assert.equal(event.getType(), 'http://gedcomx.org/Marriage');
518
  assert.equal(event.getDate().getFormal(), '+2002-06-06');
519
  assert.equal(event.getPlace().getOriginal(), 'Her town, MN');
520
  assert.equal(event.getRoles().length, 1);
521
  assert.equal(event.getRoles()[0].getPerson().getResource(), 'http://groom');
522
  assert.equal(event.getRoles()[0].getType(), 'http://gedcomx.org/Participant');
523
  
524
  assert.equal(gedx.getDocuments().length, 1);
525
  var doc = gedx.getDocuments()[0];
526
  assert.equal(doc.getType(), 'http://gedcomx.org/Abstract');
527
  assert.equal(doc.getExtracted(), false);
528
  assert.equal(doc.getTextType(), 'plain');
529
  assert.equal(doc.getText(), 'Lots of text');
530
  assert.equal(doc.getAttribution().getCreated().getTime(), 123456789);
531
  
532
  assert.equal(gedx.getPlaces().length, 1);
533
  var place = gedx.getPlaces()[0];
534
  assert.equal(place.getNames().length, 1);
535
  assert.equal(place.getNames()[0].getLang(), 'en');
536
  assert.equal(place.getNames()[0].getValue(), 'Pope\'s Creek, Westmoreland, Virginia, United States');
537
  assert.equal(place.getType(), 'http://identifier/for/the/place/type');
538
  assert.equal(place.getPlace().getResource(), 'http://place');
539
  assert.equal(place.getJurisdiction().getResource(), 'http://jurisdiction');
540
  assert.equal(place.getLatitude(), 27.9883575);
541
  assert.equal(place.getLongitude(), 86.9252014);
542
  assert.equal(place.getTemporalDescription().getFormal(), '+1899-01-04');
543
  assert.equal(place.getSpatialDescription().getResource(), 'http://uri/for/KML/document');
544
  
545
  assert.equal(gedx.getRecordDescriptors().length, 1);
546
  var rd = gedx.getRecordDescriptors()[0];
547
  assert.equal(rd.getId(), 'rd');
548
  assert.equal(rd.getLang(), 'en-US');
549
  assert.equal(rd.getFields().length, 1);
550
  assert.equal(rd.getFields()[0].getOriginalLabel(), 'Name');
551
  assert.equal(rd.getFields()[0].getValues().length, 1);
552
  
553
  assert.equal(gedx.getCollections().length, 1);
554
  var collection = gedx.getCollections()[0];
555
  assert.equal(collection.getId(), 'collection');
556
  assert.equal(collection.getLang(), 'en-US');
557
  assert.equal(collection.getContent()[0].getResourceType(), 'http://gedcomx.org/Record');
558
  assert.equal(collection.getTitle(), 'Collection Title');
559
  assert.equal(collection.getSize(), 183429102);
560
  assert(GedcomX.Attribution.isInstance(collection.getAttribution()));
561
  
562
  assert.jsonSchema(gedx.toJSON(), GedcomXSchema);
563
}